/*
 * duplicate_int.cpp
 * Copyright (C) 2014 libo <libo@tencent.com>
 *
 * Distributed under terms of the MIT license.
 */

#include <iostream>
#include <ext/hash_map>

using namespace __gnu_cxx;
using namespace std;

typedef hash_map<int, int> int_hash_t;
typedef hash_map<int, int>::value_type int_pair_t;

/*
 * @brief input and output should have same size
 * @return sizeof copied items
 */
int remove_duplicate(int input[], int output[], int sz) {
  if (sz < 0) 
    return -1;
  int_hash_t int_hash;
  int_hash_t::iterator it;
  int cp_idx = 0;
  for (int i = 0; i < sz; ++i) {
    it = int_hash.find(input[i]);
    if (it != int_hash.end()) {
    // found
      ++int_hash[input[i]];
      continue;
    } else {
    // not found
      int_hash[input[i]] = 1;
      output[cp_idx] = input[i];
      ++cp_idx;
    }
  }
  if (cp_idx > 0) {
    printf("output sz: %d\n", cp_idx);
    for(int i = 0; i < cp_idx; ++i) {
      printf("%d\t%d\n", output[i], int_hash[output[i]]);
    } 
  }
  return cp_idx;
};

int main(int argc, char* argv[]) {
  const int data_sz = 100;
  int input_data[data_sz];
  int output_data[data_sz];
  for (int i = 0; i < data_sz; ++i) {
    srand(time(NULL) + i);
    input_data[i] = rand() % 100;
  }
  int cp_sz = remove_duplicate(input_data, output_data, data_sz); 
}


